package com.jishuo.base.controller;

import com.jishuo.base.annotation.UserAuthority;
import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.bean.GridResult;
import com.jishuo.base.bean.Pagination;
import com.jishuo.base.bean.vo.MenuVO;
import com.jishuo.base.bean.vo.UserVO;
import com.jishuo.base.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@RequestMapping(value = "/sys/menu", name = "系统菜单")
@UserAuthority(menu = "sys/menu/index")
public class MenuController {
    @Resource
    private MenuService menuService;

    private String prefix = "sys/menu/";

    @UserAuthority
    @ResponseBody
    @RequestMapping(value = "menuTree", name = "菜单树")
    public List<MenuVO> menuTree(HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("login_user");
        return this.menuService.listMenuTree(user);
    }

    @RequestMapping(value = {"index", "index/{page}"}, name = "首页")
    public String index(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "index" : page);
    }

    @UserAuthority(menu = {"sys/menu/index","sys/role/index","sys/user/index"})
    @ResponseBody
    @RequestMapping(value = "listMenu", name = "菜单列表")
    public GridResult<MenuVO> listMenu(MenuVO menu, Pagination pagination) {
        return new GridResult<>(this.menuService.listMenu(menu, pagination));
    }

    @ResponseBody
    @RequestMapping(value = "addMenu", name = "增加菜单")
    public BaseResult addMenu(MenuVO menu) {
        return this.menuService.addMenu(menu);
    }

    @ResponseBody
    @RequestMapping(value = "saveMenu", name = "更新菜单")
    public BaseResult saveMenu(MenuVO menu) {
        return this.menuService.saveMenu(menu);
    }

    @ResponseBody
    @RequestMapping(value = "deleteMenu", name = "删除菜单")
    public BaseResult deleteMenu(String menuId) {
        return this.menuService.deleteMenu(menuId);
    }

    @ResponseBody
    @RequestMapping(value = "getMenu", name = "获取菜单")
    public BaseResult getMenu(String menuId) {
        MenuVO vo = this.menuService.getMenu(menuId);
        return (vo == null) ? new BaseResult(ErrorCodeEnum.DATA_NOT_FOUND) : new BaseResult(vo);
    }

}
